/**
 * 2016年8月11日
 */
package io;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class CloseResource {
	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(8081);
		InputStream stream = new Socket("localhost", 8081).getInputStream();
		Runnable r = () -> {
			try {
				stream.read();
			} catch (Exception e) {
				try {
					Thread.currentThread().sleep(1000);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.out.println(Thread.activeCount());
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		};
		Thread t = new Thread(r);
		t.start();

		TimeUnit.SECONDS.sleep(2);
		
		System.out.println(Thread.activeCount());
		
		System.out.println("main exit");
		stream.close();
	}
}